package com.bj58.migrate;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.bj58.migrate.condition.CycleCondtion;
import com.bj58.migrate.helper.DateHelper;
import com.bj58.migrate.helper.Helper;

public class ConditionBuilder{

	public static String DEFAULT_PATH = "/opt/apps/app/avgsalary/";

    private List< CycleCondtion > condions;
    private CycleCondtion first;

    public ConditionBuilder( List< CycleCondtion > condions ){

        this.condions = condions;
        init();
    }

    public int getTotal(){

        if( condions == null ){
            return 0;
        }

        int i = 1;

        if( condions != null ){
            for( CycleCondtion cycleCondtion : this.condions ){
                i *= cycleCondtion.getCount() == 0 ? 1 : cycleCondtion.getCount();
            }
        }

        return i;
    }

    private void init(){

        if( condions != null ){

            if( condions.size() == 1 ){
                this.first = condions.get( 0 );
            }

            for( int i = 0; i < condions.size(); i++ ){

                if( i == 0 ){
                    CycleCondtion con = condions.get( 0 );
                    con.setNextCondtion( condions.get( i + 1 ) );
                    con.setFirst();
                    this.first = con;
                }
                else if( i == condions.size() - 1 ){
                    CycleCondtion con = condions.get( i );
                    con.setPrevCondtion( condions.get( i - 1 ) );
                }
                else{
                    CycleCondtion con = condions.get( i );
                    con.setPrevCondtion( condions.get( i - 1 ) );
                    con.setNextCondtion( condions.get( i + 1 ) );
                }
            }
        }
    }

    public boolean next(){

        if( first == null ){
            return false;
        }

        return first.next();
    }

    public String getCondition(){

        if( first == null ){
            return "";
        }

		return first.getValue();// + "&param5358=0"; 放弃该单元参数，已经废弃了
    }

    public void separation(){

    }

	public void separation( String root, int foldcount ) throws Exception{

        if( foldcount == 0 ){
            return;
        }

        int total = this.getTotal();
        int partCount = total / foldcount == 0 ? 1 : ( total / foldcount );
        int partSize = 50000;

		String formatFileName = DateHelper.convertDatetoString( "yyyyMM", new Date() );
        for( int i = 1; i < foldcount + 1; i++ ){

            String rootFileNum = String.valueOf( i );
            File rootfile = Helper.seekFile( root );
			if( rootfile == null || !rootfile.exists() ){
				rootfile = new File( DEFAULT_PATH + root );
				rootfile.mkdirs();// 创建文件夹
			}
			File file = Helper.getFile( rootfile, formatFileName );

            if( file == null || !file.exists() ){
				System.out.println( rootfile.getAbsolutePath() + "下的子文件夹" + formatFileName + "不存在" );
				file = new File(rootfile,formatFileName);
				file.mkdirs();
				System.out.println( "创建新文件夹，file:" + file + ", filepath:" + ( file != null ? file.getAbsolutePath() : "" ) );
				// break;
            }
            int fragTotal = 0;

            if( i <= foldcount ){
                fragTotal = partCount;
            }
            else{
                fragTotal = total - partCount * 3;
            }

            int pageTotal = fragTotal % partSize == 0 ? fragTotal / partSize : fragTotal / partSize + 1;
            for( int page = 1; page <= pageTotal; page++ ){
                int cyccount = 0;
                int start = 0;
                if( page < pageTotal ){
                    cyccount = partSize;
                }
                else{
                    cyccount = fragTotal - ( page - 1 ) * partSize;
                }
                BufferedWriter output = null;
                try{
                    File filec = new File( file.getAbsolutePath() + "/" + rootFileNum + "_" + String.valueOf( page ) + ".txt" );
                    if( !filec.exists() ){
                        if( !filec.createNewFile() ){
                            break;
                        }
                    }
                    output = new BufferedWriter( new FileWriter( filec ) );
                    while( next() ){

                        output.write( this.getCondition() );
                        output.write( "\n" );
                        start++;
                        if( start == cyccount ){
                            break;
                        }
                    }
                }
                catch( Exception ex ){
					ex.printStackTrace();
                    break;
                }
                finally{
                    try{
                        output.close();
                    }
                    catch( IOException e ){
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void toText( String path ){

        try{
            File file = new File( path );
            if( !file.exists() ){
                if( file.createNewFile() ){
                    System.out.println( "文件创建成功！" + file.getAbsolutePath() );
                }
                else{
                    System.out.println( "文件创建失败！" );
                }
            }

            BufferedWriter output = new BufferedWriter( new FileWriter( file ) );
            if( this.next() ){
                output.write( this.getCondition() );
            }
            while( this.next() ){
                output.write( "\n" );
                output.write( this.getCondition() );
            }

            output.close();
        }
        catch( Exception e ){
            e.printStackTrace();
        }
    }

    public CycleCondtion getFirst(){

        return first;
    }

    public void setFirst( CycleCondtion first ){

        this.first = first;
    }
}
